package eSchool.Utils;

import eSchool.BL.AlunoInscrito;
import eSchool.BL.Disciplina;
import eSchool.BL.Disciplinas;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Export {

    private String commaCode = "[comma]";
  
    public Export() {
    }

    public void exportCSV(Disciplina disciplina) throws IOException {
        String fileName = disciplina.getCodDisciplina() +  "_" + disciplina.getNomeDisciplina() + ".CSV";
        BufferedWriter fOut = new BufferedWriter(new FileWriter(fileName));

        for (AlunoInscrito aluno : disciplina.getAlunoInscrito()) {
            this.save(fOut, commaCode, aluno);
        }

        if (fOut != null) {
            fOut.close();
        }
    }

    public void exportCSV(Disciplinas disciplinas) throws IOException {

        for (Disciplina disciplina : disciplinas) {
            exportCSV(disciplina);
        }
    }

    public void save(BufferedWriter fOut, String separator, AlunoInscrito alunoInscrito) throws IOException {
        fOut.write(alunoInscrito.getCodDisciplina().replace(separator, commaCode) + separator);
        fOut.write(alunoInscrito.getNumAluno().replace(separator, commaCode) + separator);
        fOut.write(alunoInscrito.getNome().replace(separator, commaCode) + separator);
        fOut.write("0");
        fOut.newLine();
    }
}
